<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;
use app\common\model\GoodsCategory;
class UserCollect extends Model
{

    use SoftDelete;

    

    // 表名
//    protected $name = 'GoodsDonate';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
    ];

    /**
     * 是否收藏/足迹
     * $typeid 值 $type 类型:1=商品,2=店铺 $status 类型:1=收藏,2=足迹 $state 商品类型:1=捐增,2=爆品,3=兑换
     * */
    public static function is_collect($user_id,$typeid='',$type=1,$status=1,$state=1){
        $whe['typeid'] = $typeid;
        $whe['type'] = $type;
        if($type==1){
            $whe['state'] = $state;
        }
        $whe['status'] = $status;
        if(!empty($user_id)){  $whe['uid'] = $user_id; }
        //收藏

        return $is_collect= UserCollect::where($whe)
            ->whereNull('deletetime')
            ->count();
    }
    /**
     * 收藏/足迹、新增
     * $typeid 值 $type 类型:1=商品,2=店铺 $status 类型:1=收藏,2=足迹 $state 商品类型:1=捐增,2=爆品,3=兑换
     * */
    public static function UserCollectAdd($user_id,$typeid='',$type=1,$status=1,$state=1){
        $data['uid'] =$user_id;
        $data['typeid'] =$typeid;
        $data['type'] =$type;
        $data['status'] =$status;
        if($type==1){
            $data['state'] = $state;
        }
        $info = self::where($data)->find();
        if(!$info){
            $info =  self::create($data);
        }else{
            $info->createtime = time();
            $info->updatetime = time();
            $info->save();
        }

        return $info;
    }



}
